// PartFunctionDB.cpp: implementation of the PartFunctionDB class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "phsshare.h"
#include "PartFunctionDB.h"
#include "partDBReaderInfo.h"
#include "PartDBReader.h"
#include "DBConnectionManager.h"
#include "PartFormat.h"
#include "ProductCatalogManager.h"
#include "JointPortFormer.h"
#include "FunctionRegister.h"
#include "JointFunction.h"
#include "GeneralFunctionReader.h"
#include "PersistentFacade.h"
#include "KnownPartSet.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

using namespace JointSystem;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

PartFunctionDB::PartFunctionDB(Part* pPart, _ConnectionPtr& pConn)
{
	ASSERT(pPart != NULL);
	m_pPart = pPart;
	m_pCon = pConn;
}

PartFunctionDB::~PartFunctionDB()
{

}

BOOL PartFunctionDB::CreatePart()
{
	try
	{
		if(!GetFunctionSet())
		{
			return FALSE;
		}
		
		FunctionObject* pFunction = NULL;
		pFunction = GetFirstFunction();
		while(pFunction != NULL)
		{
			m_pPart->AttachFunction(pFunction);
			pFunction = GetNextFunction();
		}
		CloseFunctionSet();
	}
	CATCH_COM_ERROR(m_FunctionSet, FALSE);
	return GetJointFunction();
}

BOOL PartFunctionDB::GetFunctionSet()
{
	CString strSql;
	GetFunctionTable();
	strSql.Format(_T("select * from %s where Part_ID = '%s'%s"), m_strFunctionTable, m_pPart->GetCustomID()
		, GetParentFunction(m_pPart->GetCustomID(), m_pPart->GetStandard()));

	const _ConnectionPtr& pCon = this->GetFunciontDB();
	m_FunctionSet = pCon->Execute(_bstr_t(strSql), NULL, adCmdText);

	return TRUE;
}

void PartFunctionDB::GetFunctionTable()
{
	ASSERT(m_pPart);

	ProductCatalog* pCatalog = ProductCatalogManager::Instance()->GetProductCatalog(m_pPart->GetStandard());
	ASSERT(pCatalog);
	Standard* pStandard = GetPersistentFacade().GetStandardGenerator()->Generate(pCatalog);
	ASSERT(pStandard);
	
	m_strFunctionTable = pStandard->FunctionTable();
}

CString PartFunctionDB::GetParentFunction(const CString& strPartID, const CString& strStandard)
{
	ASSERT(!strPartID.IsEmpty());
	ASSERT(!strStandard.IsEmpty());

	PartSortPropertySet* pSet = KnownPartSet::Instance().GetPartSortPropertySet(strStandard);
	ASSERT(pSet);

	PartSortProperty* pSPro = pSet->GetPartSortProperty(strPartID);

	if(pSPro == NULL || pSPro->Parent().IsEmpty())
	{
		return CString();
	}

	return _T("or Part_ID = '") + pSPro->Parent() + _T("'") + GetParentFunction(pSPro->Parent(), strStandard);
}

const _ConnectionPtr& PartFunctionDB::GetFunciontDB()
{
	return this->m_pCon;

}

BOOL PartFunctionDB::GetJointFunction()
{
	FunctionObject* pObject = NULL;

	ASSERT(!m_pPart->GetCustomID().IsEmpty());
	ASSERT(!m_pPart->GetStandard().IsEmpty());

	ProductCatalog* pCatalog = NULL;
	pCatalog = ProductCatalogManager::Instance()->GetProductCatalog(m_pPart->GetStandard());
	ASSERT(pCatalog);

	PartFormat* pFirstFormat = pCatalog->GetPartFormat(m_pPart->GetCustomID());
	if(pFirstFormat == NULL)
		return FALSE;
	ASSERT(pFirstFormat);

	JointPortFormersOfPartFormat* pFirstPortFormats = pFirstFormat->GetJointPortFormers();
	if(pFirstPortFormats == NULL)
		return TRUE;
	ASSERT(pFirstPortFormats);
	
	FunctionRegisterInfo* pInfo = GetFunctionRegister().Get(FunctionObject::eJOINTFUNCTION);
	ASSERT(pInfo);
	if(pInfo == NULL)
		return FALSE;
	for(int i = 0; i < pFirstPortFormats->GetCount(); i++)
	{
		JointPortFormer* pFirstPortFormat = pFirstPortFormats->GetAt(i);
		ASSERT(pFirstPortFormat);
		pObject = pInfo->CreateObject();
		static_cast<JointFunction*>(pObject)->JointStyle(atoi(pFirstPortFormat->JointPortFormatID()));
		static_cast<JointFunction*>(pObject)->JointPortID(pFirstPortFormat->SerialNumber());
		
		m_pPart->AttachFunction(pObject);
	}
	
	return TRUE;
}
